# -*- Mode: Python -*- vi:si:et:sw=4:sts=4:ts=4:syntax=python

import os

from fnmatch import fnmatch

from cerbero.build.filesprovider import FilesProvider
from cerbero.tools.libtool import LibtoolLibrary


class Recipe(recipe.Recipe):
    name = 'cdparanoia'
    version = '10.2'
    stype = SourceType.TARBALL
    url = 'xiph://%(name)s/%(name)s-III-%(version)s.src.tgz'
    tarball_dirname = '%(name)s-III-%(version)s'
    tarball_checksum = '005db45ef4ee017f5c32ec124f913a0546e77014266c6a1c50df902a55fe64df'
    autoreconf = True
    # binaries are GPL and we don't distribute them
    licenses = [License.LGPLv2_1Plus]
    allow_parallel_build = False
    config_sh = 'CFLAGS="$CFLAGS -fPIC" ./configure'
    patches = ['cdparanoia/0001-configure.in-Always-use-AC_PROG_CC.patch']

    files_libs = ['libcdda_paranoia', 'libcdda_interface']
    files_devel = ['include/cdda_interface.h', 'include/cdda_paranoia.h', 'include/utils.h']

    # No rule check
    make_check = None

    def post_install(self):
        if self.config.target_platform == Platform.LINUX:
            # Fix the libs permissions to 0755 to make the rpm packaging happy.
            # Setting for all linux distros as this may affect suse as well.
            # It will not affect debian packaging as it will run dh_fixperms
            # which does the right thing for debian.
            libs = self.files_list_by_category(FilesProvider.LIBS_CAT)
            for lib in libs:
                f = os.path.join(self.config.prefix, lib)
                if os.path.isfile(f) and not os.path.islink(f) and \
                   fnmatch(f, '*.so.*'):
                    os.chmod(f, 0o755)

        LibtoolLibrary('cdda_interface', None, None, None,
                       self.config.libdir, self.config.target_platform).save()
        LibtoolLibrary('cdda_paranoia', None, None, None,
                       self.config.libdir, self.config.target_platform,
                       deps=['cdda_interface']).save()
        super().post_install()
